//*******************************************************************
//  Die.java       Author: Jason M. Campbell
//
//  Represents a single Die object of a pair of dice.
//*******************************************************************

import javax.swing.*;
import java.awt.*;

public class Die extends JComponent
{
	private final int MAX = 6;  // maximum face value
	private final int SIDELENGTH = 60;  // for size of dice
	
	private int faceValue;  // current value showing on die
	private Dot d1, d2, d3, d4, d5, d6, d7; // dots for the die
	
	//---------------------------------------------------------------
	//  Constructor: Sets the initial face value for each die.
	//---------------------------------------------------------------
	public Die()
	{
		setPreferredSize(new Dimension(SIDELENGTH, SIDELENGTH));
		roll(); // set a random initial value
		
		d1 = new Dot(12, Color.black, 5, 5); // upper left
		d2 = new Dot(12, Color.black, 5, 24); // middle left
		d3 = new Dot(12, Color.black, 5, 43); // lower left
		d4 = new Dot(12, Color.black, 43, 5); // upper right
		d5 = new Dot(12, Color.black, 43, 24); // middle right
		d6 = new Dot(12, Color.black, 43, 43); // lower right
		d7 = new Dot(12, Color.black, 24, 24); // middle middle
	} // end Die constructor
	
	//---------------------------------------------------------------
	//  This essentially draws the dots on the Die object.
	//---------------------------------------------------------------
	public void paintComponent(Graphics g)
	{
		g.setColor(Color.white);
		g.fillRect(0, 0, SIDELENGTH, SIDELENGTH);g.setColor(Color.black);
		g.drawRect(0, 0, SIDELENGTH-1, SIDELENGTH-1);
		
		switch(faceValue)
		{
			case 1:
				d7.drawDot(g);
				break;
			case 2:
				d1.drawDot(g);
				d6.drawDot(g);
				break;
			case 3:
				d1.drawDot(g);
				d6.drawDot(g);
				d7.drawDot(g);
				break;
			case 4:
				d1.drawDot(g);
				d3.drawDot(g);
				d4.drawDot(g);
				d6.drawDot(g);
				break;
			case 5:
				d1.drawDot(g);
				d3.drawDot(g);
				d4.drawDot(g);
				d6.drawDot(g);
				d7.drawDot(g);
				break;
			case 6:
				d1.drawDot(g);
				d2.drawDot(g);
				d3.drawDot(g);
				d4.drawDot(g);
				d5.drawDot(g);
				d6.drawDot(g);
				break;
		} // end switch
	} // end paintComponent	

	//---------------------------------------------------------------
	//  Randomly assigns a value from 1 to 6 for the Die object.
	//---------------------------------------------------------------
	public void roll()
	{
		faceValue = (int)(Math.random() * MAX) + 1;
	} // end roll
	
	//---------------------------------------------------------------
	//  Face value mutator.
	//---------------------------------------------------------------
	public void setFaceValue(int value)
	{
		faceValue = value;
	} // end setFaceValue

	//---------------------------------------------------------------
	//  Face value accessor.
	//---------------------------------------------------------------
	public int getFaceValue()
	{
		return faceValue;
	} // end getFaceValue

	//---------------------------------------------------------------
	//  Dice size accessor.
	//---------------------------------------------------------------
	public int getSideLength()
	{
		return SIDELENGTH;
	} // end getSideLength

	//---------------------------------------------------------------
	//  Returns a string representation of the die.
	//---------------------------------------------------------------
	public String toString()
	{
		return "Die's value: " + faceValue + "\n";
	} // end toString
} // end class Die